// pages/infoFix/infoFix.js
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: null,
    weight: null,
    phone: '',
    date: '请选择',
    region: '请选择',
    sex: null,
    isSport: null,
    isGourmand: null,
    hasDiabetes: null,
    hasFamilyDiabetes: null,
    bmi: null,
    id: null,
    userId: null,
    avatarID: null,
    diabetesType: null,
    hipline: null,
    trueName: '',
    waist: null,
    // 模态框的信息
    title: '信息更新',
    msg: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var params = {};
    var _this = this;
    util.httpGet("/wx/userInfoEdit",params,(res) => {
      console.log(res)
      var address = [res.province, res.city, res.distinct];
      _this.setData({
        height: res.userInfo.height,
        weight: res.userInfo.weight,
        phone: res.userInfo.phone,
        date: res.birthday,
        region: address,
        sex: res.userInfo.sex,
        isSport: res.userInfo.isSport,
        isGourmand: res.userInfo.isGourmand,
        hasDiabetes: res.userInfo.hasDiabetes,
        hasFamilyDiabetes: res.userInfo.hasFamilyDiabetes,
        bmi: res.userInfo.bmi,
        id: res.userInfo.id,
        userId: res.userInfo.userId,
        avatarID: res.userInfo.avatarID,
        diabetesType: res.userInfo.diabetesType,
        hipline: res.userInfo.hipline,
        trueName: res.userInfo.trueName,
        waist: res.userInfo.waist,
      })
    })
  },
  heightChanged: function(e){
    this.setData({
      height: e.detail.value*1
    })
  },
  weightChanged: function(e){
    this.setData({
      weight: e.detail.value*1
    })
  },
  phoneChanged: function(e){
    this.setData({
      phone: e.detail.value
    })
  },

  DateChanged(e) {
    this.setData({
      date: e.detail.value
    })
  },
  RegionChanged: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  sexChanged: function(e){
    this.setData({
      sex: e.detail.value=='true'?true:false
    })
  },
  isSportChanged: function(e){
    this.setData({
      isSport: e.detail.value=='true'?true:false
    })
  },
  isGourmandChanged: function(e){
    this.setData({
      isGourmand: e.detail.value=='true'?true:false
    })
  },
  hasDiabetesChanged: function(e){
    this.setData({
      hasDiabetes: e.detail.value=='true'?true:false
    })
  },
  hasFamilyDiabetesChanged: function(e){
    this.setData({
      hasFamilyDiabetes: e.detail.value=='true'?true:false
    })
  },

  submitInfo: function(){
    var that = this;
    var province = this.data.region[0],
        city = this.data.region[1],
        distinct = this.data.region[2];
    var params = {
      _birthday: this.data.date,
      height: this.data.height,
      weight: this.data.weight,
      phone: this.data.phone,
      province: province,
      city: city,
      distinct: distinct,
      sex: this.data.sex,
      isSport: this.data.isSport,
      isGourmand: this.data.isGourmand,
      hasDiabetes: this.data.hasDiabetes,
      hasFamilyDiabetes: this.data.hasFamilyDiabetes,
      bmi: this.data.bmi,
      id: this.data.id,
      userId: this.data.userId,
      trueName: this.data.trueName
    }
    util.httpPost("/wx/ajaxUserInfoEdit",params,(res)=>{
      that.setData({
        msg: res.msg
      });
      this.showModal()
    })
  },

  showModal() {
    this.setData({
      modalName: 'Modal'
    })
  },
  hideModal() {
    this.setData({
      modalName: null
    })
  },
})